package action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import dicts.CountryDict;
import entities.TIps;
import interfaces.IIpsService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.IpUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class IpsAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IIpsService ipsService;

	private TIps ipsBean;
	private List<TIps> ipsList;

	private String ipsNo;
	private String ipsNos;
	private String ipsType;
	private String ipsName;
	private String ipsCountry;
	private String ipsNumber;
	private String ipsMin;
	private BigDecimal ipsMinnum;
	private String ipsMax;
	private BigDecimal ipsMaxnum;
	private String ipsState;
	private Date ipsDate;
	private Date ipsDateTop;
	private Date ipsDateEnd;
	private String ipsRemark;

	// 展示对象
	public String show() throws Exception {
		if (!ipsService.exist(ipsNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			ipsBean = ipsService.fetch(ipsNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!ipsService.exist(ipsNo)) {
			ipsBean = new TIps();
			ipsBean.setIpsState("1");
		} else {
			ipsBean = ipsService.fetch(ipsNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!ipsService.exist(ipsNo)) {
			ipsBean = new TIps();
			ipsBean.setIpsNo(StringUtil.guid());
		} else {
			ipsBean = ipsService.fetch(ipsNo);
		}
		ipsBean.setIpsType(ipsType);
		ipsBean.setIpsName(CountryDict.COUNTRY_MAP.get(ipsCountry)[1]);
		ipsBean.setIpsCountry(ipsCountry);
		ipsBean.setIpsMin(ipsMin);
		ipsBean.setIpsMinnum(ipsMinnum);
		ipsBean.setIpsMax(ipsMax);
		ipsBean.setIpsMaxnum(ipsMaxnum);
		ipsBean.setIpsState(ipsState);
		ipsBean.setIpsDate(new Date());
		ipsBean.setIpsRemark(ipsRemark);

		ipsService.persist(ipsBean);
		return SUCCESS;
	}

	// 批量删除
	public String move() throws Exception {
		for (String ipsNo : StringUtil.split(ipsNos)) {
			ipsService.remove(ipsNo);
		}
		return SUCCESS;
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("ipsNo", StringUtil.split(ipsNo));
		paramMap.put("ipsType", ipsType);
		paramMap.put("ipsCountry", ipsCountry);
		if (!StringUtil.isEmpty(ipsNumber)) {
			paramMap.put("ipsMinnum", IpUtil.ip2number(ipsNumber));
			paramMap.put("ipsMaxnum", IpUtil.ip2number(ipsNumber));
		}
		paramMap.put("ipsState", ipsState);
		paramMap.put("ipsDate", ParameterUtil.dateCombin(ipsDateTop, ipsDateEnd));
		paramMap.put("ipsRemark", ipsRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "ipsMin" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TIps> resultSet = ipsService.search(querySet);
		ipsList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("ipsNo", StringUtil.split(ipsNo));
		paramMap.put("ipsType", ipsType);
		paramMap.put("ipsCountry", ipsCountry);
		if (!StringUtil.isEmpty(ipsNumber)) {
			paramMap.put("ipsMinnum", IpUtil.ip2number(ipsNumber));
			paramMap.put("ipsMaxnum", IpUtil.ip2number(ipsNumber));
		}
		paramMap.put("ipsState", ipsState);
		paramMap.put("ipsDate", ParameterUtil.dateCombin(ipsDateTop, ipsDateEnd));
		paramMap.put("ipsRemark", ipsRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "ipsDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TIps> resultSet = ipsService.search(querySet);
		ipsList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("ip类型");
		fieldHead.add("名称");
		fieldHead.add("所属国家");
		fieldHead.add("起始IP");
		fieldHead.add("起始IP值");
		fieldHead.add("结束IP");
		fieldHead.add("结束IP值");
		fieldHead.add("是否可用");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TIps ipsBean : ipsList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(ipsBean.getIpsNo());
			fieldItem.add(ipsBean.getIpsType());
			fieldItem.add(ipsBean.getIpsName());
			fieldItem.add(ipsBean.getIpsCountry());
			fieldItem.add(ipsBean.getIpsMin());
			fieldItem.add(ipsBean.getIpsMinnum().toString());
			fieldItem.add(ipsBean.getIpsMax());
			fieldItem.add(ipsBean.getIpsMaxnum().toString());
			fieldItem.add((ipsBean.getIpsState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(ipsBean.getIpsDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(ipsBean.getIpsDate(), DateUtil.TIME));
			fieldItem.add(ipsBean.getIpsRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	public void random() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		if (StringUtil.isEmpty(ipsCountry) || StringUtil.isEmpty(ipsType)) {
			response.getWriter().println("缺少生成条件");
			return;
		}
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("ipsCountry", ipsCountry);
		paramMap.put("ipsType", ipsType);
		ipsBean = ipsService.dynamic(paramMap);
		if (!StringUtil.isEmpty(ipsBean)) {
			if (ipsType.equals("IPV4")) {
				ipsName = IpUtil.number2ipv4(NumberUtil.random(ipsBean.getIpsMinnum().toBigInteger(), ipsBean.getIpsMaxnum().toBigInteger()).longValue());
			}
			if (ipsType.equals("IPV6")) {
				ipsName = IpUtil.number2ipv6(NumberUtil.random(ipsBean.getIpsMinnum().toBigInteger(), ipsBean.getIpsMaxnum().toBigInteger()));
			}
		}
		response.getWriter().println(ipsName);
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IIpsService getIpsService() {
		return ipsService;
	}

	public void setIpsService(IIpsService ipsService) {
		this.ipsService = ipsService;
	}

	public TIps getIpsBean() {
		return ipsBean;
	}

	public void setIpsBean(TIps ipsBean) {
		this.ipsBean = ipsBean;
	}

	public List<TIps> getIpsList() {
		return ipsList;
	}

	public void setIpsList(List<TIps> ipsList) {
		this.ipsList = ipsList;
	}

	public String getIpsNo() {
		return ipsNo;
	}

	public void setIpsNo(String ipsNo) {
		this.ipsNo = ipsNo;
	}

	public String getIpsNos() {
		return ipsNos;
	}

	public void setIpsNos(String ipsNos) {
		this.ipsNos = ipsNos;
	}

	public String getIpsType() {
		return ipsType;
	}

	public void setIpsType(String ipsType) {
		this.ipsType = ipsType;
	}

	public String getIpsName() {
		return ipsName;
	}

	public void setIpsName(String ipsName) {
		this.ipsName = ipsName;
	}

	public String getIpsCountry() {
		return ipsCountry;
	}

	public void setIpsCountry(String ipsCountry) {
		this.ipsCountry = ipsCountry;
	}

	public String getIpsMin() {
		return ipsMin;
	}

	public void setIpsMin(String ipsMin) {
		this.ipsMin = ipsMin;
	}

	public String getIpsMax() {
		return ipsMax;
	}

	public void setIpsMax(String ipsMax) {
		this.ipsMax = ipsMax;
	}

	public String getIpsState() {
		return ipsState;
	}

	public void setIpsState(String ipsState) {
		this.ipsState = ipsState;
	}

	public Date getIpsDate() {
		return ipsDate;
	}

	public void setIpsDate(Date ipsDate) {
		this.ipsDate = ipsDate;
	}

	public Date getIpsDateTop() {
		return ipsDateTop;
	}

	public void setIpsDateTop(Date ipsDateTop) {
		this.ipsDateTop = ipsDateTop;
	}

	public Date getIpsDateEnd() {
		return ipsDateEnd;
	}

	public void setIpsDateEnd(Date ipsDateEnd) {
		this.ipsDateEnd = ipsDateEnd;
	}

	public String getIpsRemark() {
		return ipsRemark;
	}

	public void setIpsRemark(String ipsRemark) {
		this.ipsRemark = ipsRemark;
	}

	public BigDecimal getIpsMinnum() {
		return ipsMinnum;
	}

	public void setIpsMinnum(BigDecimal ipsMinnum) {
		this.ipsMinnum = ipsMinnum;
	}

	public BigDecimal getIpsMaxnum() {
		return ipsMaxnum;
	}

	public void setIpsMaxnum(BigDecimal ipsMaxnum) {
		this.ipsMaxnum = ipsMaxnum;
	}

	public String getIpsNumber() {
		return ipsNumber;
	}

	public void setIpsNumber(String ipsNumber) {
		this.ipsNumber = ipsNumber;
	}

}
